package com.kgc.scd.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.scd.mapper.Collectmapper;
import com.kgc.scd.mapper.FootMapper;
import com.kgc.scd.bean.*;
import com.kgc.scd.service.CollectionService;
import com.kgc.scd.util.PageSupport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created On : 2022/6/14.
 * <p>
 * Author     : 15950526365
 * <p>
 * Description: 收藏商品业务实现类
 */

@Slf4j
@Service
public class CollectionServiceImpl implements CollectionService {

    @Autowired(required = false)
    Collectmapper collectmapper;

    @Autowired(required = false)
    FootMapper footMapper;


    @Override
    public boolean addCollection(Collect collect) {
        return collectmapper.insertSelective(collect) > 0;
    }

    @Override
    public boolean delCollection(String userId, String proId) {
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andCollUidEqualTo(userId).andCollProidEqualTo(proId);
        return collectmapper.deleteByExample(collectExample) > 0;
    }

    @Override
    public boolean delCollectList(String userId, List<String> proIds) {
        for (String proId : proIds){
            CollectExample collectExample = new CollectExample();
            CollectExample.Criteria criteria = collectExample.createCriteria();
            criteria.andCollUidEqualTo(userId).andCollProidEqualTo(proId);
            collectmapper.deleteByExample(collectExample);
        }
        return true;
    }

    /**
     * @author : 98730
     * @date   : 2022/6/13
     * @param  : [java.lang.Integer, java.lang.Integer, java.lang.String]
     * @return : com.kgc.scd.util.PageSupport<com.kgc.scd.bean.CartItem>
     * @description : 根据用户id查询用户购物车列表，支持分页操作
     */
    @Override
    public PageSupport<CollectProducts> queryCartListByPage(Integer pageNo, Integer pageSize, String userId, Integer orderBy) {
        // 分页
        PageHelper.startPage(pageNo,pageSize);
        List<CollectProducts> collectProducts = collectmapper.selectDetailByUserId(userId,orderBy);

        PageInfo<CollectProducts> pageInfo = new PageInfo<>(collectProducts);
        PageSupport<CollectProducts> pageSupport = new PageSupport<>();
        // 设置分页内容
        pageSupport.setPageData(collectProducts);
        // 设置当前页码
        pageSupport.setPageNo(pageNo);
        // 设置每页条数
        pageSupport.setPageSize(pageSize);
        // 设置总页数
        pageSupport.setTotalPage(pageInfo.getPages());
        // 设置总记录数
        pageSupport.setTotalCount((int) pageInfo.getTotal());
        return pageSupport;
    }

    @Override
    public PageSupport<Foots> selectFoots(Integer pageNo, Integer pageSize, String userId) {
        // 分页
        PageHelper.startPage(pageNo,pageSize);
        List<Foots> foots = collectmapper.selectFoots(userId);
        log.info("查询结果为：{}", foots);

        PageInfo<Foots> pageInfo = new PageInfo<>(foots);
        PageSupport<Foots> pageSupport = new PageSupport<>();
        // 设置分页内容
        pageSupport.setPageData(foots);
        // 设置当前页码
        pageSupport.setPageNo(pageNo);
        // 设置每页条数
        pageSupport.setPageSize(pageSize);
        // 设置总页数
        pageSupport.setTotalPage(pageInfo.getPages());
        // 设置总记录数
        pageSupport.setTotalCount((int) pageInfo.getTotal());
        log.info("返回结果：{}", pageSupport);
        return  pageSupport;
    }

    @Override
    public void deleteProId(String userId,String prodId) {
//        QueryWrapper<Foot> wrapper = new QueryWrapper<>();
//        wrapper.eq("foot_userid",userId);
//        wrapper.eq("foot_proid",prodId);
        footMapper.deleteprodAndUser(userId,prodId);

    }

    @Override
    public void addProId(Foot foot) {
        footMapper.insert(foot);
    }

    @Override
    public Foots queryProdByFood(String proId) {
        return footMapper.selectProductByProd(proId);
    }

    @Override
    public List<Integer> queryFootByProId(String userId) {
        List<Integer> prods = footMapper.selectProds(userId);
        for (Integer p:prods){

            System.out.println(p);
        }
        return prods;
    }

    /*
     * @author : 15950526365
     * @date   : 2022/6/20
     * @param  : [java.lang.String]
     * @return : boolean
     * @description : 清除足迹
     */
    @Override
    public boolean
    clearFoot(String userId) {
        return footMapper.clearFoot(userId);
    }
    /*
     * @author : 15950526365
     * @date   : 2022/6/20
     * @param  : [java.lang.String]
     * @return : boolean
     * @description : 清除收藏
     */
    @Override
    public boolean clearCollection(String userId) {
        return footMapper.clearCollect(userId) ;
    }


}
